package com.netease.financial.base.suspendwindow;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.netease.financial.common.util.NfsLog;
import com.netease.financial.common.util.PackageUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by wyf on 10/9/15.
 */
public class ChatBallHelper {

    private static final String TAG = ChatBallHelper.class.getSimpleName();
    private static final String META_KEY_CHAT_BALL_WHITE_LIST = "CHAT_BALL_WHITE_LIST";

    // 黑名单的优先级,大于白名单
    private final Set<String> mWhiteLists;
    private final Set<String> mBlackLists;

    private Handler mHandler;
    private ChatBall mChatBall;
    private Context mContext;

    private static final class NestedInstanceClass {
        private static final ChatBallHelper INSTANCE = new ChatBallHelper();
    }

    private ChatBallHelper() {
        mWhiteLists = new HashSet<>();
        mBlackLists = new HashSet<>();
        mBlackLists.add("com.netease.financial.base.splash.SplashActivity");
    }

    protected static ChatBallHelper getInstance() {
        return NestedInstanceClass.INSTANCE;
    }

    protected void init(Context context) {
        if (null == mContext) {
            mHandler = new Handler(context.getMainLooper());

            mContext = context.getApplicationContext();
            mChatBall = new ChatBall(mContext);
            initActivityListForSuspendWindow(mContext);
        }
    }


    protected void onConfigurationChanged() {
        mChatBall.onConfigurationChanged();
    }

    /**
     * 需要展示悬浮框的Activity名字，只有注册过的Activity才能显示悬浮框
     *
     * @param activityName
     */
    protected void register(String activityName) {
        NfsLog.d("TooltipService", "register");
        if (!TextUtils.isEmpty(activityName)) {
            mWhiteLists.add(activityName);
        }
    }

    protected void onResume(String activityName) {
        if (mBlackLists.contains(activityName)) {
            return;
        }
        if (mWhiteLists.contains(activityName)) {
            // 如果此activity需要显示悬浮球,则取消延迟执行的悬浮球隐藏任务(如果此任务存在)
            mHandler.removeCallbacks(mHideChatBallRunnable);
            // 显示悬浮球
            mChatBall.showChatBall();
        }
    }

    protected void onPause() {
        // 延迟100毫秒执行,在此期间如果跳转到了需要显示悬浮球的activity,则取消此任务
        mHandler.postDelayed(mHideChatBallRunnable, 100L);
    }

    protected void setOnChatBallClickListener(View.OnClickListener listener) {
        mChatBall.setOnChatBallClickListener(listener);
    }

    protected void setUnreadMessageCount(int count) {
        mChatBall.setUnreadMessageCount(count);
    }

    private void initActivityListForSuspendWindow(Context context) {
        String mActivityNames = PackageUtils.getStringMetaData(context, META_KEY_CHAT_BALL_WHITE_LIST);
        if (!TextUtils.isEmpty(mActivityNames)) {
            String[] names = mActivityNames.split("\\|");
            for (String name : names) {
                if (!TextUtils.isEmpty(name)) {
                    mWhiteLists.add(name);
                    NfsLog.v(TAG, " onCreate Activity:" + name);
                }
            }
        }
    }

    private Runnable mHideChatBallRunnable = new Runnable() {
        @Override
        public void run() {
            mChatBall.hideChatBall();
        }
    };

}
